package com.example.demo.config;

import com.example.demo.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class AdminInterceptor implements HandlerInterceptor {



    //这个方法是在访问接口之前执行的，我们只需要在这里写验证登陆状态的业务逻辑，就可以在用户调用指定接口之前验证登陆状态了
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        //判断用户的token信息是否满足
        //String token = request.getHeader("authorization");
        //boolean token_result = TokenUtils.verify(token);
        User user = (User) request.getSession().getAttribute("user");
        if (user != null){
            return true;
        }
        //当前不满足条件,直接跳转拦截
        response.sendRedirect("/front/login");
        return false;
    }

}
